package com.cc.openeye

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.tencent.kuikly.compose.coil3.rememberAsyncImagePainter
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.background
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.Row
import com.tencent.kuikly.compose.foundation.layout.Spacer
import com.tencent.kuikly.compose.foundation.layout.fillMaxSize
import com.tencent.kuikly.compose.foundation.layout.height
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.foundation.layout.size
import com.tencent.kuikly.compose.foundation.shape.RoundedCornerShape
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.setContent
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.graphics.Brush
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.platform.LocalActivity
import com.tencent.kuikly.compose.ui.text.TextStyle
import com.tencent.kuikly.compose.ui.toComposeColor
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.compose.ui.unit.sp
import com.tencent.kuikly.core.annotations.Page
import com.tencent.kuikly.core.module.RouterModule
import com.tencent.kuikly.core.module.SharedPreferencesModule
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.tencent.kuikly.core.pager.Pager
import com.tencent.kuikly.core.utils.urlParams
import com.cc.openeye.base.BasePager
import com.cc.openeye.base.Utils
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth

@Page("router", supportInLocal = true)
internal class ComposeRoutePager : BasePager() {

    override fun willInit() {
        super.willInit()
        setContent {
            ComposeRouteImpl()
        }
    }

    companion object {
        const val PLACEHOLDER = "输入pageName（不区分大小写）"
        const val CACHE_KEY = "router_last_input_key2"
        const val LOGO = "https://vfiles.gtimg.cn/wuji_dashboard/xy/componenthub/Dfnp7Q9F.png"
        const val JUMP_TEXT = "跳转"
        const val TEXT_KEY = "text"
        const val TITLE = "Kuikly页面路由"
        const val AAR_MODE_TIP = "如：router 或者 router&key=value （&后面为页面参数）"
    }
}

@Composable
internal fun ComposeRouteImpl() {
    var textFieldValue by remember { mutableStateOf("") }
    val localPager = LocalActivity.current.getPager() as Pager
    val statusBarHeight = LocalActivity.current.pageData.statusBarHeight
    Column(
        modifier = Modifier.padding(top = (statusBarHeight + 15).dp).fillMaxSize()
            .background(Color.White),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Button(
            modifier = Modifier.fillMaxWidth().height(45.dp).padding(start = 15F, end = 15F)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "openEyeFindPage")
            }
        ) {
            Text("开眼发现", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15F).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "ComposeVideoDemo")
            }
        ) {
            Text("video", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15f).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "dynamicPage")
            }
        ) {
            Text("dynamicPage", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15f).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "TestStatePage")
            }
        ) {
            Text("TestStatePage", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15f).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "TestDialogPage")
            }
        ) {
            Text("TestDialogPage", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15f).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "ViewTestPage")
            }
        ) {
            Text("ViewTestPage", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15f).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "KuiklyDynamicPage")
            }
        ) {
            Text("KuiklyDynamicPage", color = Color.White)
        }

        Button(
            modifier = Modifier.fillMaxWidth().padding(start = 15F, end = 15F, top = 15f).height(45.dp)
                .backgroundColor(Color.Blue)
                .borderRadius(5.dp),
            onClick = {
                jumpPage(localPager, "CeilingPage")
            }
        ) {
            Text("CeilingPage", color = Color.White)
        }
    }
}

fun jumpPage(pager: Pager, inputText: String) {
    val params = urlParams("pageName=$inputText")
    val pageData = JSONObject()
    params.forEach {
        pageData.put(it.key, it.value)
    }
    val pageName = pageData.optString("pageName")
    pager.acquireModule<RouterModule>(RouterModule.MODULE_NAME).openPage(pageName, pageData)
}